package com.pelli.imarket.client.panels;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.TreePanelEvent;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.extjs.gxt.ui.client.widget.Component;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.EditorGrid;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanelSelectionModel;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.ui.Composite;
import com.gwtext.client.util.JSON;
import com.pelli.imarket.client.DataEntryTreeCreator;
import com.pelli.imarket.client.models.DataEntryTreeModel;
import com.pelli.imarket.client.models.test.Folder;
import com.pelli.imarket.client.store.JsonModelData;
import com.pelli.imarket.client.widget.GridManager;

public class HomePanel extends Composite {

	public HomePanel() {

		final ContentPanel mainPanel = new ContentPanel();
		mainPanel.setHeading("Home Page");
		mainPanel.setLayout(new FitLayout());

		// cntntpnlHomePage.setTopComponent(menuBar);

		final ContentPanel secondPanel = new ContentPanel();
		secondPanel.setHeaderVisible(false);
		secondPanel.setHeading("New ContentPanel");
		secondPanel.setCollapsible(true);
		secondPanel.setLayout(new BorderLayout());
		secondPanel.setBottomComponent(new PagingToolBar(50));

		Folder model = DataEntryTreeCreator.getTree();

		TreeStore<ModelData> store = new TreeStore<ModelData>();
		store.add(model.getChildren(), true);

		final TreePanel treePanel = new TreePanel(store);

		treePanel.setSelectionModel(new TreePanelSelectionModel<ModelData>() {

			@Override
			protected void onMouseClick(TreePanelEvent e) {
				// TODO Auto-generated method stub
				if (e.getNode().getModel() instanceof DataEntryTreeModel) {
					DataEntryTreeModel em = (DataEntryTreeModel) e.getNode()
							.getModel();
					System.out.println(em.getId() + ": " + em.getName());
					GridManager.generateGrid(mainPanel, secondPanel, em.getId());
				}

			}



		});

		secondPanel.add(treePanel, new BorderLayoutData(LayoutRegion.WEST));

		treePanel.setBorders(true);

		treePanel.setDisplayProperty("name");
		treePanel.setWidth(250);

		mainPanel.add(secondPanel);

		initWidget(mainPanel);
		mainPanel.setSize("1280px", "800px");
		setSize("1280", "800");
	}

}
